﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.Configuration;

using Training.Library.DataAccess.Administration.UserManagement;
using Training.Library.DomainObject.Administration.UserManagement;
//using Training.Library.DomainObject.Administration.Catalog;
//using Training.Library.DataAccess.Administration.Catalog;

using Training.Web.Support;
using Training.Library.DataAccess.Administration.Catalog;
using Training.Library.DomainObject.Administration.Catalog;

namespace Training.Web.Forms.UserManagement
{
    public partial class sUserform : BasePage
    {
        private int id = -1;
        private int status = -1;
        
        protected override void OnLoad(EventArgs e)
        {
            try
            {
                hfStatus.Value = status.ToString();
                txtUserCode.Focus();
                if (Request.Params["Id"] != null)
                {
                    id = Convert.ToInt32(Request.Params["Id"]);
                }
                //if (Request.Params["Status"] != null)
                //{
                //    status = Convert.ToInt32(Request.Params["Status"]);
                //    hfStatus.Value = status.ToString();
                //}

                CabangRepo CabangDal = new CabangRepo(ConfigurationManager.ConnectionStrings["TrainingConn"].ConnectionString);
                List<Cabang> list = CabangDal.GetList();
                var query = list.OrderBy(x => x.NamaCabang);
                list = query.ToList();
                ddlCabang.Items.Clear();

                list.ForEach(delegate(Cabang obj)
                {
                    ddlCabang.Items.Add(new ListItem(obj.NamaCabang, obj.CabangID.ToString()));
                });

                ddlCabang.Items.Insert(0, new ListItem("-- pilih cabang --", string.Empty));


                if (!Page.IsPostBack)
                {

                    if (id > 0) // Edit an existing item
                    {
                        txtUserName.Focus();
                        recUserRepo userDal = new recUserRepo(ConfigurationManager.ConnectionStrings["TrainingConn"].ConnectionString);
                        recUser dto = userDal.GetById(id);

                        if (dto != null)
                        {

                            hfID.Value = id.ToString();
                            txtUserName.Text = dto.UserName;
                            txtUserCode.Text = dto.UserCode;
                            //txtEmail.Text = dto.Email;
                            //txtMobile.Text = dto.NIK;
                            chkIsActive.Checked = dto.IsActive.HasValue ? dto.IsActive.Value : false;

                            trPassword.Visible = false;
                            trConfirmPassword.Visible = false;
                            txtUserCode.ReadOnly = true;
                            txtUserCode.Enabled = false;

                            if (dto.CabangID != null)
                            {
                                ddlCabang.Items.FindByValue(dto.CabangID.ToString()).Selected = true;

                                KaryawanRepo KaryawanDal = new KaryawanRepo(ConfigurationManager.ConnectionStrings["TrainingConn"].ConnectionString);
                                List<Karyawan> listKaryawan = KaryawanDal.GetListByCabangAndLokasi(Convert.ToInt32(dto.CabangID), Convert.ToInt32(dto.LokasiID));
                                var queryKaryawan = listKaryawan.OrderBy(x => x.NIK);
                                listKaryawan = queryKaryawan.ToList();
                                ddlNIK.Items.Clear();

                                listKaryawan.ForEach(delegate(Karyawan obj)
                                {
                                    ddlNIK.Items.Add(new ListItem(obj.NIK + " - " + obj.NamaKaryawan, obj.NIK.ToString()));
                                });

                                ddlNIK.Items.Insert(0, new ListItem("-- pilih NIK --", string.Empty));

                                ddlNIK.Items.FindByValue(dto.NIK.ToString()).Selected = true;
                            }

                            LokasiRepo LokasiDal = new LokasiRepo(ConfigurationManager.ConnectionStrings["TrainingConn"].ConnectionString);
                            List<Lokasi> listLokasi = LokasiDal.GetList();
                            var queryLokasi = listLokasi.Where(x=>x.CabangID==dto.CabangID).OrderBy(x => x.NamaLokasi);
                            listLokasi = queryLokasi.ToList();
                            ddlLokasi.Items.Clear();

                            listLokasi.ForEach(delegate(Lokasi obj)
                            {
                                ddlLokasi.Items.Add(new ListItem(obj.NamaLokasi, obj.LokasiID.ToString()));
                            });

                            ddlLokasi.Items.Insert(0, new ListItem("-- pilih lokasi --", string.Empty));

                            ddlLokasi.Items.FindByValue(dto.LokasiID.ToString()).Selected = true;
                        }
                    }

                    GetPrivilege();

                }
            }
            catch
            {
            }

        }

        private void GetPrivilege()
        {
            Membership membership = new Membership(ConfigurationManager.ConnectionStrings["TrainingConn"].ConnectionString);

            List<recAccessRights.AccessRights> accessRights = membership.GetAccessRights(base.UserID, base.MenuID);

            accessRights.ForEach(delegate(recAccessRights.AccessRights access)
            {
                switch (access)
                {
                    case (recAccessRights.AccessRights.View):
                        ClientScript.RegisterStartupScript(typeof(Page), "view", "$('#btnSave').button('disable');", true);
                        break;
                    case (recAccessRights.AccessRights.Add):
                        ClientScript.RegisterStartupScript(typeof(Page), "add", "$('#btnSave').button('enable');", true);
                        break;
                    case (recAccessRights.AccessRights.Edit):
                        ClientScript.RegisterStartupScript(typeof(Page), "edit", "$('#btnSave').button('enable');", true);
                        break;
                    case (recAccessRights.AccessRights.NoAccess):
                        var url = Helper.GetAppVirtualPath(Page);
                        ClientScript.RegisterStartupScript(typeof(Page), "noAccess", "alert('Anda tidak mempunyai hak akses ke halaman ini!'); window.location = '" + url + "'", true);
                        break;
                }
            });

        }
    
    }
}

